<template>
	<jk-dialog
		v-model="showDialog"
		title="详情-设备保养项目"
		width="900px"
		:show-content-loading="showLoading"
		append-to-body
		:show-close-btn="true"
		:show-confirm-btn="false"
		:show-cancel-btn="false"
		@on-close="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
	>
		<div ref="box1">
			<el-form ref="ruleForm" :model="ruleForm" label-width="90px" :show-message="false">
				<el-row class="flex-start-center-wrap">
					<el-col :span="8">
						<el-form-item class="margin-bottom-10" label="区域">
							<span class="read-only-2">{{ ruleForm.workshopName }}</span>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item class="margin-bottom-10" label="区域">
							<span class="read-only-2">{{ ruleForm.workshopAreaName }}</span>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item class="margin-bottom-10" label="日期">
							<span class="read-only-2">{{ ruleForm.belongDate }}</span>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item class="margin-bottom-10" label="班次">
							<span class="read-only-2">{{ ruleForm.scheduleShiftName }}</span>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item class="margin-bottom-10" label="班组">
							<span class="read-only-2">{{ ruleForm.scheduleGroupName }}</span>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item class="margin-bottom-10" label="人员">
							<span class="read-only-2">{{ ruleForm.empName }}</span>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</div>
		<el-row :gutter="10">
			<el-col :span="14">
				<div class="option-item-title">开台品种</div>
				<jk-table :footer-method="footerMethod" :show-footer="true" :height="dialogTableHeight + 42" border :data="ruleForm.productList">
					<vxe-column title="序号" align="center" type="seq" :width="60" />
					<vxe-column title="规格" align="left" field="productModels" :min-width="120" />
					<vxe-column title="批号" align="left" field="batchCode" :min-width="100"></vxe-column>
					<vxe-column title="上机数" align="right" field="upQty" :width="80" />
					<vxe-column title="关台数" align="right" field="closeQty" :width="80" />
					<vxe-column title="实际开台" align="right" field="openQty" :width="80" />
				</jk-table>
			</el-col>
			<el-col :span="10">
				<div class="option-item-title">人员考勤</div>
				<jk-table :footer-method="footerMethod" :show-footer="true" :height="dialogTableHeight + 42" border :data="ruleForm.empList">
					<vxe-column title="岗位" align="left" field="postName" :min-width="100" />
					<vxe-column title="到岗数" align="right" field="totalQty" :min-width="70" />
					<vxe-column title="请假数" align="right" field="zhiGuan" :min-width="100"></vxe-column>
				</jk-table>
			</el-col>
		</el-row>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import { spinReportProductOpenRecordGet } from '../../../../api/production/workshopAreaOpenRecord';
    import XEUtils from 'xe-utils';
    export default {
        name: 'WorkshopAreaOpenRecord',
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        mixins: [calHeight],
        data() {
            return {
                activeTab: '0',
                showDialog: false,
                showLoading: true,
                ruleForm: {}
            };
        },
        methods: {
            footerMethod({ columns, data }) {
                const keys = ['upQty', 'closeQty', 'openQty', 'totalQty', 'zhiGuan'];
                return [
                    columns.map((column, columnIndex) => {
                        if (columnIndex === 0) {
                            return '合计';
                        }
                        if (keys.includes(column.field)) {
                            return XEUtils.sum(data, column.field);
                        }
                        return null;
                    })
                ];
            },
            mergeColMethod({ row, _rowIndex, column, visibleData }) {
                const fields = ['machineName']; // 根据字段判断数据相同的合并
                const cellValue = row[column.property];
                if (cellValue && fields.includes(column.property)) {
                    const prevRow = visibleData[_rowIndex - 1];
                    let nextRow = visibleData[_rowIndex + 1];
                    if (prevRow && prevRow[column.property] === cellValue) {
                        return { rowspan: 0, colspan: 0 };
                    } else {
                        let countRowspan = 1;
                        while (nextRow && nextRow[column.property] === cellValue) {
                            nextRow = visibleData[++countRowspan + _rowIndex];
                        }
                        if (countRowspan > 1) {
                            return { rowspan: countRowspan, colspan: 1 };
                        }
                    }
                }
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                this.showLoading = true;
                return spinReportProductOpenRecordGet({ id: this.dataId }).then(res => {
                    this.showLoading = false;
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res;
                    }
                });
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDetailRequest();
                }
            }
        }
    };
</script>
